#include "postdata.h"
const char* host = "39.106.160.132";
String url = "http://39.106.160.132/lhlsite/laravel/public/admin/info/post_reveive";
const char OneNetServer[] = "39.106.160.132";

void WiFiClient_POST(char tem[5]) {

    const int httpPort = 80;                //Server服务端的端口号
    // 用HTTP协议连接OneNET服务器
    WiFiClient client;
    DHT dht(2, DHT11);
    dht.begin();
    //用WiFiClient类创建 client 对象，实现HTTP协议连接OneNET服务器
    if (!client.connect(host, httpPort)) {       //若连接 Onenet服务器 失败就等5秒，重新连接
        Serial.println("WiFiClient connection failed");
        delay(5000);                            //等5秒
        return;                                //程序返回到loop重新开始
    }




    if (client.connected()) {              //若连接成功，就发送POST 请求上报数据
        Serial.println("connection succ");
        
        float Humidity = dht.readHumidity();
        float Temperature = dht.readTemperature();
        String data = "{\"" + String("PostData") +"\":" + String(Temperature)+", \"" + String("PostData2") + "\":" + String(Humidity)+"}";
        Serial.println(data);

        // 创建发送指令 -- We now combine the request to the server
        String post_data = "POST" + url + "HTTP/1.1\r\n" +
                           "Host:" + OneNetServer + "\r\n" +
                           "Content-Length: " + String(data.length()) + "\r\n" +                     //发送数据长度
                           "Connection: close\r\n\r\n" +
                           data;
        Serial.println(post_data);
        // 发送指令 -- This will send the request to server
        client.print(post_data);


        Serial.println("send succ");
    }
    // 关闭HTTP客户端
    client.stop();
    delay(5000);
}
